package com.gopay.bis.service.bank.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.bis.manager.bank.BisExpayDtlManager;
import com.gopay.bis.service.bank.BisExpayDtlService;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.SmsAuthMarkConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BisBankExpayDtl;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MD5Util;

/** 
 * 快捷流水实现类
 * @ClassName: BisExpayDtlServiceImpl 
 * @author w_jiqing 
 * @date 2017年6月26日 下午4:22:54  
 */ 
@Service("bisExpayDtlService")
public class BisExpayDtlServiceImpl implements BisExpayDtlService {

    @Resource(name = "bisExpayManager")
    private BisExpayDtlManager bisExpayManager;

    @Override
    public BisBankExpayDtl addExpayDtl(ExPayBaseMessage base) throws GopayException {

        BisBankExpayDtl expayDtl = new BisBankExpayDtl();
        expayDtl.setOrderId(base.getOrderId());
        String elements = "";
        if ((BisBankBusinessProd._15.value).equals(base.getBankBusProd().value)) {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId();
        } else {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId()
                    + base.getAvailableDate() + base.getCvv2();
        }
        String enementsValue = MD5Util.sign(elements);
        expayDtl.setElementsValue(enementsValue);
        expayDtl.setOrderStatus(SmsAuthMarkConstants.CER_MARK_SUCCESS);
        bisExpayManager.addBisExpayDtl(expayDtl);
        return expayDtl;
    }
    
    @Override
    public void updateSmsApplyDtl(ExPayBaseMessage base) throws GopayException {
        BisBankExpayDtl expayDtl = bisExpayManager.getExpayDtlByOrderId(base.getOrderId());
        if (null == expayDtl) {
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6103);

        }
        expayDtl.setOrderStatus(SmsAuthMarkConstants.APPLY_MARK_SUCCESS);
        bisExpayManager.updateExpayDtl(expayDtl);
    }

    @Override
    public void updateSmsAuthDtl(ExPayBaseMessage base) throws GopayException {
        BisBankExpayDtl expayDtl = bisExpayManager.getExpayDtlByOrderId(base.getOrderId());
        if (null == expayDtl) {
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6103);

        }
        expayDtl.setOrderStatus(SmsAuthMarkConstants.AUTH_MARK_SUCCESS);
        bisExpayManager.updateExpayDtl(expayDtl);
    }
    
    
    @Override
    public void checkSendSmsCerElements(ExPayBaseMessage base) throws GopayException {
        BisBankExpayDtl expayDtl = bisExpayManager.getExpayDtlByOrderId(base.getOrderId());
        String elements = "";
        if ((BisBankBusinessProd._15.value).equals(base.getBankBusProd().value)) {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId();
        } else {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId()
                    + base.getAvailableDate() + base.getCvv2();
        }
        String enementsValue = MD5Util.sign(elements);
        if (null == expayDtl) {
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6103);

        } else if (!enementsValue.equals(expayDtl.getElementsValue())) {

            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6104);
        }
        
    }

    @Override
    public void checkSmsCerElements(ExPayBaseMessage base) throws GopayException {
        List<BisBankExpayDtl> lists = bisExpayManager.getExpayDtlByOrderIdList(base.getOrderId(), SmsAuthMarkConstants.APPLY_MARK_SUCCESS);
        if(lists == null || lists.size() <= 0){
            // 没有短信发送成功的记录  ->快捷支付未经短信申请
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6105);
        }
        String elements = "";
        if ((BisBankBusinessProd._15.value).equals(base.getBankBusProd().value)) {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId();
        } else {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId()
                    + base.getAvailableDate() + base.getCvv2();
        }
        String enementsValue = MD5Util.sign(elements);
        boolean exceptionMark = true;
        for(BisBankExpayDtl expayDtl: lists){
            //有要素校验成功的记录
            if(enementsValue.equals(expayDtl.getElementsValue())){
                exceptionMark= false;
                break;
            }
        }
        if(exceptionMark){
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6104);
        }       
    }

    @Override
    public void checkPayCerElements(ExPayBaseMessage base) throws GopayException {
        BisBankExpayDtl expayDtl = bisExpayManager.getExpayDtlByOrderId(base.getOrderId());
        String elements = "";
        if ((BisBankBusinessProd._15.value).equals(base.getBankBusProd().value)) {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId();
        } else {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId()
                    + base.getAvailableDate() + base.getCvv2();
        }
        String enementsValue = MD5Util.sign(elements);
        if (null == expayDtl) {
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6103);

        } else if (!enementsValue.equals(expayDtl.getElementsValue())) {

            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6104);

        } else if (!SmsAuthMarkConstants.AUTH_MARK_SUCCESS.equals(expayDtl.getOrderStatus())) {

            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6106);
        }

    }

    @Override
    public List<BisBankExpayDtl> getBisBankExpayDtlList(ExPayBaseMessage base, String status) throws GopayException {
        
        return bisExpayManager.getExpayDtlByOrderIdList(base.getOrderId(), status);
    }

    @Override
    public List<BisBankExpayDtl> getBisBankExpayDtlListbyElements(ExPayBaseMessage base, String status)
            throws GopayException {
        String elements = "";
        if ((BisBankBusinessProd._05.value).equals(base.getBankBusProd().value)) {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId()
            + base.getAvailableDate() + base.getCvv2();
        } else {
            elements = base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId();
        }
        String enementsValue = MD5Util.sign(elements);
        return bisExpayManager.getExpayDtlByOrderIdAndElementsList(base.getOrderId(), enementsValue, status);
    }


  

}
